package com.university.mentalhealthplatform.teacherV2.service;

import com.university.mentalhealthplatform.appModels.Article;
import com.university.mentalhealthplatform.teacherV2.repository.ArticleRepositoryV2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceV2 {

    @Autowired
    private ArticleRepositoryV2 articleRepository;

    public List<Article> getAllArticles() {
        try {
            List<Article> articles = articleRepository.findAll();
            if (articles.isEmpty()) {
                System.out.println("No articles found");
            }
            return articles;
        } catch (Exception e) {
            e.printStackTrace(); // 记录数据库操作的错误
            throw e; // 将异常抛出以便处理
        }
    }

    public Article getArticleById(Long id) {
        return articleRepository.findById(id).orElse(null);
    }
}